package mt.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import mt.entity.User;
import mt.mapper.UserMapper;
import mt.service.UserService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public int register(String username, String password) {
        boolean exists = baseMapper.exists(Wrappers.lambdaQuery(User.class).eq(User::getUsername, username));
        if (exists) {
            return -1;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return baseMapper.insert(user);
    }

    @Override
    public int login(String username, String password) {
        User one = baseMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername, username));
        if (one == null) {
            return -1;
        }
        if (!one.getPassword().equals(password)) {
            return 0;
        }

        return 1;
    }
}
